package com.ostrich.cloud.ostrichPermission.app.controller;


import com.ostrich.cloud.common.enums.UserTypeEnum;
import com.ostrich.cloud.common.result.Result;
import com.ostrich.cloud.ostrichPermission.client.PermissionClient;
import com.ostrich.cloud.ostrichPermission.client.param.AddUserParam;
import com.ostrich.cloud.ostrichPermission.client.param.UpdateUserStatusParam;
import com.ostrich.cloud.ostrichPermission.infra.model.Perm;
import com.ostrich.cloud.ostrichPermission.infra.model.Role;
import com.ostrich.cloud.ostrichPermission.infra.model.UserParent;

import com.ostrich.cloud.ostrichPermission.service.impl.UserParentServiceImpl;
import com.ostrich.cloud.ostrichUserService.client.CustomerMangerClient;
import com.ostrich.ostrichUserService.param.LoginCusParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2022-10-26
 */
@RestController
@Slf4j
public class UserParentController implements PermissionClient {
    @Resource
    private UserParentServiceImpl userParentService;
    @Resource
    private CustomerMangerClient customerMangerClient;
    @Override
    public Result addUser(@RequestBody AddUserParam param) {
        UserParent user = new UserParent();
        user.setUsername(param.getUsername());
        user.setPassword(param.getPassword());
        user.setStatus(UserTypeEnum.NORMAL.getCode());
        Integer addUser = userParentService.addUser(user, param.getRoleId());
        if(addUser==0){
            return Result.error();
        }
        return Result.ok(addUser);
    }
    @Override
    public Result selectByPid(@RequestParam("parentId") Integer parentId) {

        UserParent userParent = userParentService.selectUserByPid(parentId);
        if(userParent!=null){
            return Result.ok(userParent);
        }
        return Result.okm("没有此用户");
    }

    @Override
    public Result UpdateUserStatus(@RequestBody UpdateUserStatusParam updateUserStatusParam) {
        String s = userParentService.updateUserStatus(updateUserStatusParam.getParentId(), updateUserStatusParam.getStatus());
        return Result.okm(s);
    }

    @Override
    public List<Role> getRoleListByPid(@RequestParam("parentId") Integer pid) {

        List<Role> roles = userParentService.selctRoleListByPid(pid);
        log.info("获取role列表");
        return roles;
    }

    @Override
    public List<Perm> getPermissionListByPid(@RequestParam("parentId") Integer pid) {
        List<Perm> permissionListByPid = userParentService.getPermissionListByPid(pid);
        return permissionListByPid;
    }

    @Override
    public Result login(@RequestBody LoginCusParam loginCusParam) {
        Result result = customerMangerClient.customerLogin(loginCusParam);

        return result;
    }


}

